from product_upload.util.basic.mysql_util import db_get_one

class BasicSupplier:
    # 字段顺序与数据库字段一致
    fields = ['id', 'status', 'platform', 'code', 'name', 'create_time', 'update_time', 'update_by', 'remark']

    def __init__(self, **kwargs):
        self.id = kwargs.get('id', None)
        self.status = kwargs.get('status', None)
        self.platform = kwargs.get('platform', None)
        self.code = kwargs.get('code', "")
        self.name = kwargs.get('name', "")
        self.create_time = kwargs.get('create_time', None)
        self.update_time = kwargs.get('update_time', None)
        self.update_by = kwargs.get('update_by', None)
        self.remark = kwargs.get('remark', None)

    @classmethod
    def from_db(cls, field_names, row) -> 'BasicSupplier':
        if len(field_names) != len(row):
            raise ValueError("bug!---db field len != query field len")
        data_dict = dict(zip(field_names, row))
        return cls(**data_dict)

    def __repr__(self):
        field_info = []
        for field in self.fields:
            value = getattr(self, field)
            field_info.append(f"{field}={repr(value)}")
        return f"BasicSupplier=[{', '.join(field_info)}]"


if __name__ == '__main__':
    print(db_get_one("basic_supplier", "id=1", BasicSupplier))
    pass
